from django.db import models
from django.contrib.auth.models import AbstractUser

# Create your models here.

class UserProfile(AbstractUser):
    phone = models.CharField(max_length=13,verbose_name='电话')

'''
模版分组
'''
class TempGroup(models.Model):
    group_type = (
        ('项目','项目'),
        ('人员','人员')
        )
    ch_name = models.CharField(max_length=32,unique=True)
    en_name = models.CharField(max_length=32,unique=True)
    type = models.CharField(max_length=16,choices=group_type)

'''
项目/人员模版
'''    
class Template(models.Model):
    ch_name = models.CharField(max_length=32,verbose_name='中文名称')
    en_name = models.CharField(max_length=32,verbose_name='英文名称')
    group = models.ForeignKey('TempGroup',on_delete=models.CASCADE)
    enabled = models.BooleanField(default=False,verbose_name='已启用')
    create_time = models.DateTimeField(auto_now_add=True,verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now=True,verbose_name='更新时间')

'''
项目流程
'''
class ProFLow(models.Model):
    template = models.ForeignKey(Template,on_delete=models.CASCADE)
    ch_name = models.CharField(max_length=32,verbose_name='流程名')
    order = models.IntegerField(verbose_name='序号')
    
    class Meta:
        unique_together = ('template', 'ch_name')

'''
流程任务
'''
class FlowTask(models.Model):
    flow = models.ForeignKey(ProFLow,on_delete=models.CASCADE)
    ch_name = models.CharField(max_length=32,verbose_name='任务名')
    order = models.IntegerField(verbose_name='序号')
    
    class Meta:
        unique_together = ('flow', 'ch_name')
    
'''
子任务
'''
class SubTask(models.Model):
    project = models.CharField(max_length=255,verbose_name='项目名')
    task = models.ForeignKey(FlowTask,on_delete=models.CASCADE)
    title = models.CharField(max_length=255,verbose_name='标题')
    member = models.TextField(verbose_name='任务负责人')
    begin_date = models.DateTimeField(verbose_name='开始日期')
    end_date = models.DateTimeField(verbose_name='截止日期')
    desc = models.TextField(verbose_name='任务描述')
    log = models.TextField(blank=True,null=True,verbose_name='任务记录')
    status = models.CharField(default='wait',max_length=32,verbose_name='任务状态') # wait, run, finish
    priority = models.CharField(default='middle',max_length=8,verbose_name='优先级') # hight, middle,low
    
    @property
    def task_name(self):
        return self.task.ch_name
    
    @property
    def flow_name(self):
        return self.task.flow.ch_name
    

'''
子任务附件表
'''
class SubTaskAttach(models.Model):
    task = models.ForeignKey(SubTask,on_delete=models.CASCADE)
    upload_time = models.DateTimeField(auto_now_add=True,verbose_name='上传时间')
    attach = models.FileField(upload_to='attach',verbose_name='附件')
    
    @property
    def subtask_title(self):
        return self.task.title

'''
流程实例表
'''
class FlowInst(models.Model):
    instance = models.ForeignKey('Instance',on_delete=models.CASCADE)
    template = models.ForeignKey(Template,on_delete=models.CASCADE)
    flow = models.ForeignKey(ProFLow,on_delete=models.CASCADE)
    begin_date = models.DateTimeField(verbose_name='开始日期')
    end_date = models.DateTimeField(verbose_name='结束日期')
    duration = models.IntegerField(default=0,verbose_name='需要工时')
    to_start = models.FloatField(default=0,verbose_name='距离开工')
    status = models.CharField(max_length=32,verbose_name='流程状态') # wait, run, finish
    
    @property
    def ch_name(self):
        return self.flow.ch_name
    

'''
通知表
'''
class TaskNotify(models.Model):
    subtask = models.ForeignKey(SubTask,on_delete=models.CASCADE)
    type = models.CharField(max_length=16,verbose_name='通知类型')#start == 任务开始, end = 任务结束
    create_time = models.DateTimeField(auto_now_add=True,verbose_name='创建时间')# 即通知时间
    read = models.BooleanField(default=False,verbose_name='已读')
    
'''
项目办理记录表
'''
class ProEvent(models.Model):
    project = models.ForeignKey('Instance',on_delete=models.CASCADE,verbose_name='项目实例')
    title = models.CharField(max_length=128,unique=True,verbose_name='事件标题')
    content = models.TextField(verbose_name='办理内容')
    create_time = models.DateTimeField(auto_now_add=True,verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now=True,verbose_name='更新时间')

'''
事件的附件
'''
class EventAttach(models.Model):
    project = models.ForeignKey('Instance',on_delete=models.CASCADE,verbose_name='项目实例')
    event = models.ForeignKey(ProEvent,on_delete=models.CASCADE)
    upload_time = models.DateTimeField(auto_now_add=True,verbose_name='上传时间')
    attach = models.FileField(upload_to='event',verbose_name='附件')

'''
字段类型
'''
class FieldType(models.Model):
    ch_name = models.CharField(max_length=32,verbose_name='中文名')
    en_name = models.CharField(max_length=32,unique=True,verbose_name='英文名')#唯一标识
    field_length = models.SmallIntegerField(blank=True,null=True,verbose_name='字段长度')
    
'''
模版字段
'''
class TempField(models.Model):
    template = models.ForeignKey(Template,on_delete=models.CASCADE)
    ch_name = models.CharField(max_length=32,verbose_name='字段名')
    en_name = models.CharField(max_length=32,verbose_name='唯一标识')
    type = models.ForeignKey('FieldType',on_delete=models.PROTECT,verbose_name='字段类型')
    must = models.BooleanField(default=False,verbose_name='是否必填')
    display = models.BooleanField(default=True,verbose_name='是否显示字段')
    order = models.IntegerField(default=1,verbose_name='显示顺序')
    create_time = models.DateTimeField(auto_now_add=True,verbose_name='创建时间')
    
    @property
    def type_name(self):
        return self.type.ch_name

'''
字段模版关联表
'''
class FieldToTemp(models.Model):
    template = models.ForeignKey('Template',on_delete=models.CASCADE,verbose_name='模版')
    field = models.ForeignKey('TempField',on_delete=models.CASCADE,verbose_name='字段库')
    
'''
实例表
'''
class Instance(models.Model):
    template = models.ForeignKey(Template,on_delete=models.PROTECT)
    owner = models.ForeignKey(to=UserProfile,null=True,on_delete=models.SET_NULL)
    create_time = models.DateTimeField(auto_now_add=True,verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now=True,verbose_name='更新时间')
    
'''
short表
'''
class ShortTable(models.Model):
    instance = models.ForeignKey('Instance',on_delete=models.CASCADE,verbose_name='实例')
    field = models.CharField(max_length=64,verbose_name='所属字段')
    value = models.CharField(max_length=32,verbose_name='值')

'''
long表
'''
class LongTable(models.Model):
    instance = models.ForeignKey('Instance',on_delete=models.CASCADE,verbose_name='实例')
    field = models.CharField(max_length=64,verbose_name='所属字段')
    value = models.CharField(max_length=128,verbose_name='值')
    
'''
int表
'''
class IntTable(models.Model):
    instance = models.ForeignKey('Instance',on_delete=models.CASCADE,verbose_name='实例')
    field = models.CharField(max_length=64,verbose_name='所属字段')
    value = models.IntegerField(verbose_name='值')

'''
float表
'''
class FloatTable(models.Model):
    instance = models.ForeignKey('Instance',on_delete=models.CASCADE,verbose_name='实例')
    field = models.CharField(max_length=64,verbose_name='所属字段')
    value = models.FloatField(verbose_name='值')

'''
bool表
'''
class BoolTable(models.Model):
    instance = models.ForeignKey('Instance',on_delete=models.CASCADE,verbose_name='实例')
    field = models.CharField(max_length=64,verbose_name='所属字段')
    value = models.BooleanField(verbose_name='值')

'''
datetime表
'''
class DateTable(models.Model):
    instance = models.ForeignKey('Instance',on_delete=models.CASCADE,verbose_name='实例')
    field = models.CharField(max_length=64,verbose_name='所属字段')
    value = models.DateTimeField(verbose_name='值')

'''
enum表
'''
class EnumTable(models.Model):
    instance = models.ForeignKey('Instance',on_delete=models.CASCADE,verbose_name='实例')
    field = models.CharField(max_length=64,verbose_name='所属字段')
    value = models.CharField(max_length=64,verbose_name='值')

'''
file表
'''
class FileTable(models.Model):
    instance = models.ForeignKey('Instance',on_delete=models.CASCADE,verbose_name='实例')
    field = models.CharField(max_length=64,verbose_name='所属字段')
    value = models.FileField(upload_to='files',verbose_name='值')

'''
image表
'''
class ImageTable(models.Model):
    instance = models.ForeignKey('Instance',on_delete=models.CASCADE,verbose_name='实例')
    field = models.CharField(max_length=64,verbose_name='所属字段')
    value = models.ImageField(upload_to='images',verbose_name='值')

'''
list表
'''
class ListTable(models.Model):
    instance = models.ForeignKey('Instance',on_delete=models.CASCADE,verbose_name='实例')
    field = models.CharField(max_length=64,verbose_name='所属字段')
    value = models.TextField(verbose_name='值')

'''
存模型 enum 值
'''
class EnumValue(models.Model):
    field = models.ForeignKey('TempField',on_delete=models.CASCADE,verbose_name='所属字段')
    key = models.CharField(max_length=16,verbose_name='枚举ID')
    value = models.CharField(max_length=64,verbose_name='枚举值')